﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace SEO.Spider
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 代理服务器抓取类
    /// 完成日期：2011年6月12日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class CatchProxy
    {
        /// <summary>
        /// 抓取代理服务器搜索结果
        /// </summary>
        /// <param name="strWebUrl">网址</param>
        /// <param name="strStartMarkString">开始标记</param>
        /// <param name="strEndMarkString">结束标记</param>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strSourceName">关键词</param>
        public void AccordingURLGetResultList(string strWebUrl, string strStartMarkString, string strEndMarkString, System.Windows.Forms.RichTextBox rb, string strKeywordsName)
        {
            try
            {
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding("gb18030");
                wc.OpenRead(strWebUrl);
                string strGetHtmlCode = wc.RespHtml;

                //匹配列表源代码
                Regex rx = new Regex(strStartMarkString + "(?<content>[\\s\\S]*)?" + strEndMarkString, RegexOptions.Singleline);
                string strGetListHtmlContent = rx.Match(strGetHtmlCode).Result("${content}");
                strGetListHtmlContent = strGetListHtmlContent.Replace("</td>", "|");
                strGetListHtmlContent = strGetListHtmlContent.Replace("</tr>", "$");
                strGetListHtmlContent = SEO.Utility.StringHelper.ClearIframeAndScript(strGetListHtmlContent);
                strGetListHtmlContent = SEO.Utility.StringHelper.CleanHtml(strGetListHtmlContent);
                strGetListHtmlContent = strGetListHtmlContent.Replace("\r","");
                strGetListHtmlContent = strGetListHtmlContent.Replace("\n", "");
                strGetListHtmlContent = strGetListHtmlContent.Replace("\t", "");
                strGetListHtmlContent = strGetListHtmlContent.Replace(" ", "");
                strGetListHtmlContent = strGetListHtmlContent.Replace("IP|端口|类型|查询|地址|$", "");

                //分割每行
                string[] splitContent = strGetListHtmlContent.Split(new char[] {'$'});
                for (int i = 0; i < splitContent.Length;i++ )
                {
                    //218.18.101.98|8080|透明|whois|广东省深圳市福田区电信ADSL|
                    string strData = splitContent[i].ToString();
                    if (strData.Contains("vary"))
                    {
                        //排除广告
                    }
                    else
                    {
                        //分割代理服务器信息
                        string[] splitProxy = strData.Split(new char[] { '|' });
                        string strProxyIP = splitProxy[0];
                        string strProxyPort = splitProxy[1];

                        SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 已抓取到代理服务器 -> " + strProxyIP);
                        rb.Text = SEO.Utility.PublicValue.GetMessage();

                        SEO.DAL.ProxyDAL pydal = new DAL.ProxyDAL();
                        if (!pydal.CheckProxyIPIsExist(strProxyIP))
                        {
                            try
                            {
                                //加入数据库
                                SEO.BLL.ProxyBLL pybll = new BLL.ProxyBLL();
                                SEO.Model.ProxyIP ip = new Model.ProxyIP();
                                ip.ProxyID = pydal.GetAutoAddNum();
                                ip.ProxyIPAddress = strProxyIP;
                                ip.ProxyPort = int.Parse(strProxyPort);
                                pybll.InsertProxyIPBLL(ip);
                            }
                            catch (Exception ex)
                            {
                                SEO.Utility.Log.SaveErrorLog("ProxyIP加入数据库 -> " + ex.Message);
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                string strError = "百度搜索结果列表抓取匹配失败，关键词：" + strKeywordsName + "\r\n" + ex.Message;
                SEO.Utility.Log.SaveErrorLog("抓取百度搜索结果入口 -> " + strError);
            }

        }
    }
}
